package com.cloud.spring.date;


import lombok.extern.log4j.Log4j2;

import java.time.*;
import java.time.temporal.TemporalAdjusters;

@Log4j2
public class DateDemo {
    public static void main(String[] args){
        /**
         * LocalDate
         */
        //获取当前时间
        LocalDate today=LocalDate.now();
        log.info(today);

        //根据年月日生成时间
        LocalDate date1=LocalDate.of(2016,12,1);
        log.info(date1);

        //根据字符串生成日期
        LocalDate date2=LocalDate.parse("2017-12-11");
        log.info(date2);

        //计算本月最后一天
        LocalDate date3=today.with(TemporalAdjusters.lastDayOfMonth());
        log.info(date3);

        //取今年的第111天
        LocalDate date4=today.withDayOfYear(111);
        log.info(date4);

        //取2015年的第一个星期天
        LocalDate date5=LocalDate.parse("2015-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));
        log.info(date5);
        //获取年份
        log.info(" 年份： "+date5.getYear()+" 月份： "+date5.getMonthValue()+" 日： "+date5.getDayOfMonth());
        /**
         * 日期加减
         */
        //100天后
        log.info(date5.plusDays(100));
        //四个月后
        log.info(date5.plusMonths(4));
        //2年后
        log.info(date5.plusYears(2));
        //一个星期后
        log.info(date5.plusWeeks(43));

        /**
         * LocalTime
         */
        //当前时间
        LocalTime now=LocalTime.now();
        log.info(now);
        //清除毫秒
        log.info(now.withNano(0));
        //根据时分秒生成时间
        log.info(LocalTime.of(6,29,39));
        //字符串转时间
        log.info(LocalTime.parse("12:34:17"));
        //获取当前小时，分钟
        log.info(now.getHour()+"  "+now.getMinute());
        /**
         * 时间加减
         */
        //2小时后
        log.info(now.plusHours(2).withNano(0));
        //23分钟后
        log.info(now.plusMinutes(23).withNano(0));
        /**
         * 日期转星期
         */
        DayOfWeek dayOfWeek=LocalDate.parse("2014-12-22").getDayOfWeek();
        log.info(dayOfWeek);
        //判断日期之前或者之后
        boolean isAfter=date1.isAfter(date2);
        log.info(isAfter);
        /**
         * 计算两个日期之间包含多少天
         */
        Period period=Period.between(today,date1);
        log.info(period.getDays());
        /**
         * MonthDay YearMonth
         */
        MonthDay monthDay=MonthDay.of(date1.getMonth(),date1.getDayOfMonth());
        YearMonth yearMonth=YearMonth.now();
        log.info(monthDay);
        log.info(yearMonth);
    }
}
